package com.thunbu.thunbu_meeting_system.viewMdel

import androidx.lifecycle.MutableLiveData
import com.hjq.toast.ToastUtils
import com.thunbu.lib_common.base.CoroutineViewModel
import com.thunbu.thunbu_meeting_system.await
import com.thunbu.thunbu_meeting_system.launch
import com.thunbu.thunbu_meeting_system.service.ApiService
import com.thunbu.thunbu_meeting_system.service.MainNet
import com.thunbu.thunbu_meeting_system.toRequestBody
import com.thunbu.thunbu_meeting_system.utils.RetrofitTransUtil
import com.wu.add.AddImagesInfo
import java.util.*

class InventoryAddVM : CoroutineViewModel<Unit>() {

    val success = MutableLiveData<Boolean>()


    override fun initModel() {

    }

    fun insertDiskSurplus(assetStatus: Int,stockTaskCode:String?, type: String?, zc: String?, brand: String?, model: String?, user: String?, department: String?, address: String?, remark: String?,shuliang:String?, items: ArrayList<AddImagesInfo>?) {
        if(type.isNullOrEmpty()){
            ToastUtils.show("请选择资产分类")
            return
        }
        if(zc.isNullOrEmpty()){
            ToastUtils.show("请输入资产名称")
            return
        }
        if(brand.isNullOrEmpty()){
            ToastUtils.show("请输入品牌")
            return
        }
        if(model.isNullOrEmpty()){
            ToastUtils.show("请输入型号")
            return
        }
        if(user.isNullOrEmpty()){
            ToastUtils.show("请选择使用人")
            return
        }
        if(department.isNullOrEmpty()){
            ToastUtils.show("请选择使用部门")
            return
        }
        if(address.isNullOrEmpty()){
            ToastUtils.show("请选择存放地点")
            return
        }
        if(remark.isNullOrEmpty()){
            ToastUtils.show("请输入备注")
            return
        }
        if(shuliang.isNullOrEmpty()){
            ToastUtils.show("请输入数量")
            return
        }
//        if(items.isNullOrEmpty()){
//            ToastUtils.show("请上传图片")
//            return
//        }
        launch({
            var images:String? = null
            if(!items.isNullOrEmpty()){
                val prefix = mutableMapOf("prefix" to "yipandian/")
                val map2RequestBody = RetrofitTransUtil.map2RequestBody(prefix as Map<String, Any>?)
                val list = items.map { it.imgUrl as String }.filter { it.isNotEmpty() }
                val map2Multipart = RetrofitTransUtil.map2Multipart(list)
                val result = MainNet.server(ApiService::class.java).uploadOssFiles(map2RequestBody, map2Multipart).await(true, true)
                if(!result.isNullOrEmpty()){
                    images = result.joinToString()
                }
            }
            //todo 缺少资产类型和使用状态字段
            var map = mutableMapOf<String,Any?>(
                    "assetStatus" to assetStatus,
                    "assetRemark" to remark,
                    "zichanflmc" to type,
                    "zichanmc" to zc,
                    "pinpai" to brand,
                    "guigexh" to model,
                    "shiyongrid" to user,
                    "shiyongglbmid" to department,
                    "cunfangddid" to address,
                    "stockTaskCode" to stockTaskCode,
                    "shuliang" to shuliang,
            )
            if(!images.isNullOrEmpty()){
                map[images] = images
            }
            val result = MainNet.server(ApiService::class.java).insertDiskSurplus(map.toRequestBody()).await(true,true)
            success.value = true
        })
    }



}